package com.softme.trade.engine.setup;

import java.util.List;

import com.softme.trade.model.Role;
import com.softme.trade.model.dao.RoleDAO;
import com.softme.trade.util.BeanFactory;

/**Classe criada para fazer o setup de permissões padrões do sistema
 * 
 * @author Mews
 *
 */
public class SetupRole implements Setup
{
	RoleDAO dao = (RoleDAO) BeanFactory.getBean("roleDAO", RoleDAO.class);
	
	public boolean canRun()
	{
		List<Role> list = this.dao.loadAll();
		
		if (list.size() >= 7)
			return false;
		
		return true;
	}

	public boolean run()
	{
		addRole(new Long(1), "ROLE_NONE", "roleNoneDescription", "none");
		addRole(new Long(2), "ROLE_CUSTOMER", "roleCustomerDescription", "customer");
		addRole(new Long(3), "ROLE_COMERCIAL", "roleComercialDescription", "commercial");
		addRole(new Long(4), "ROLE_FINANCIAL", "roleFinancialDescription", "financial");
		addRole(new Long(5), "ROLE_INTERNAL_OPERATIONAL", "roleInternalOperationDescription", "internalOperation");
		addRole(new Long(6), "ROLE_FULL_OPERATIONAL", "roleFullOperationDescription", "fullOperational");
		addRole(new Long(7), "ROLE_ADM", "roleAdmDescription", "administrator");

		return true;
	}

	private void addRole(Long id, String code, String description, String name)
	{
		Role role = new Role();
		role.setId(id);
		role.setName(name);
		role.setDescription(description);
		role.setCode(code);
		
		//faz o commit do novo objeto
		dao.save(role);
	}
}
